from transitions import State, Machine


class Matter:

    def say_hello(self):
        print('hello, new state!')

    def say_goodbye(self):
        print('goodbye, old state!')

    def on_enter_liquid(self):
        print('We have just entered state liquid!')

    def __init__(self):
        """不定型物体

        固态→熔化→液态
        液态→蒸发→气态
        固态→升华→气态
        气态→电离→等离子态
        """
        states = [
            State(name='solid', on_enter='say_hello', on_exit='say_goodbye'),
            State(name='liquid', on_enter='say_hello', on_exit='say_goodbye'),
            State(name='gas', on_enter='say_hello', on_exit='say_goodbye'),
            State(name='plasma', on_enter='say_hello', on_exit='say_goodbye'),
        ]
        # 这种方式需要人为的编写 不够优雅  应该是自动获取enter exit事件  没有的话就不要
        self.machine = Machine(model=self, states=states, initial=states[0])
        self.machine.add_transition(trigger='melt', source='solid', dest='liquid')
        self.machine.add_transition(trigger='evaporate', source='liquid', dest='gas')
        self.machine.add_transition(trigger='sublimate', source='solid', dest='gas')
        self.machine.add_transition(trigger='ionize', source='gas', dest='plasma')


lump = Matter()
print(lump.state)  # solid

lump.melt()
# goodbye, old state!
# hello, new state!
# We have just entered state liquid!

print(lump.state)  # liquid
print(lump.is_solid())  # Fasle
print(lump.is_liquid())  # True
